package test.client.thread;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import test.client.support.thread.ThreadListener;
import test.client.support.time.TimeTracker;

public abstract class BaseThread implements Runnable {
	protected Log log = LogFactory.getLog(this.getClass());
	
	private List<TimeTracker> timeTrackers = new LinkedList<TimeTracker>();
	
	private ThreadListener threadListener;
	
	private int id = 0;
	
	private Status status = Status.PREPARED;
	private Result result;
	private String message;
	
	public enum Status {
		PREPARED, RUNNING, STOPPED, COMPLETED;
	}
	
	public enum Result {
		SUCCESS, FAILURE;
	}
	
	@Override
	public abstract void run();
	
	public void stop() {
		this.status = Status.STOPPED;
		this.result = Result.FAILURE;
	}
	
	protected void notifyStarted() {
		if (this.threadListener != null) threadListener.hasStarted();
	}

	protected void notifyStopped() {
		if (this.threadListener != null) threadListener.hasCompleted();
	}
	
	/*
	 * Time Tracking Methods
	 */
	protected TimeTracker addTimeTracker() {
		TimeTracker tracker = new TimeTracker();
		timeTrackers.add(tracker);
		
		return tracker;
	}
	
	/*
	 * Getters and Setters
	 */
	public List<TimeTracker> getTimeTrackers() {
		return timeTrackers;
	}
	public ThreadListener getThreadListener() {
		return threadListener;
	}
	public void setThreadListener(ThreadListener threadlistener) {
		this.threadListener = threadlistener;
	}
	
	public int getId() {
		return this.id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public Result getResult() {
		return result;
	}
	public void setResult(Result result) {
		this.result = result;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}
